package com.lin.views;



import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;


public class RoundedImageView extends ImageView {
	private int mBorderThickness = 0;
	private Context mContext;
	private int mBorderColor = 0xFFFFFFFF;
	public RoundedImageView(Context context) {
		super(context);
		mContext = context;
	}

	public RoundedImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		setCustomAttributes(attrs);
	}

	public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		setCustomAttributes(attrs);
	}

	private void setCustomAttributes(AttributeSet attrs) {
		mBorderThickness = 0;
		mBorderColor = 0;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		try {
			Drawable drawable = getDrawable();
			if (drawable == null) {
				return;
			}

			if (getWidth() == 0 || getHeight() == 0) {
				return;
			}
			this.measure(0, 0);
			if(drawable.getClass() == NinePatchDrawable.class)
				return;

			Bitmap b = ((BitmapDrawable) drawable).getBitmap();
			if(b == null){
//				b= BitmapFactory.decodeResource(getResources(), R.drawable.defaul_personal);
			}
			Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

			int w = getWidth(), h = getHeight();

			int radius = (w < h ? w : h) / 2 - mBorderThickness;
			if(bitmap.getWidth() == 0 || bitmap.getHeight() == 0) return;
			Bitmap roundBitmap = getCroppedBitmap(bitmap, radius);

			final Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setFilterBitmap(true);
			paint.setDither(true);
			paint.setColor(mBorderColor);
			canvas.drawCircle(w / 2,
					h / 2, radius + mBorderThickness, paint);
			canvas.drawBitmap(roundBitmap, w / 2 - radius, h /2 - radius, null);
		} catch(NullPointerException e) {


			/*Drawable drawable = mContext.getResources().getDrawable(R.drawable.defaul_personal);
			if (drawable == null) {
				return;
			}

			if (getWidth() == 0 || getHeight() == 0) {
				return;
			}
			this.measure(0, 0);
			if(drawable.getClass() == NinePatchDrawable.class)
				return;

			Bitmap b = ((BitmapDrawable) drawable).getBitmap();
			if(b == null){
				b= BitmapFactory.decodeResource(getResources(), R.drawable.defaul_personal);
			}
			Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

			int w = getWidth(), h = getHeight();

			int radius = (w < h ? w : h) / 2 - mBorderThickness;
			if(bitmap.getWidth() == 0 || bitmap.getHeight() == 0) return;
			Bitmap roundBitmap = getCroppedBitmap(bitmap, radius);

			final Paint paint = new Paint();
			paint.setAntiAlias(true);
			paint.setFilterBitmap(true);
			paint.setDither(true);
			paint.setColor(mBorderColor);
			canvas.drawCircle(w / 2,
					h / 2, radius + mBorderThickness, paint);
			canvas.drawBitmap(roundBitmap, w / 2 - radius, h /2 - radius, null);*/
		}
	}

	public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) throws IllegalArgumentException {
		Bitmap scaledSrcBmp = null;
		int diameter = radius * 2;
		if (bmp.getWidth() != diameter || bmp.getHeight() != diameter)
			if(bmp.getWidth() > 0 && bmp.getHeight() > 0) {
				Log.i("datainfo", "1");
				Log.i("datainfo", "width:" + bmp.getWidth() + ", height:" + bmp.getHeight());
				Log.i("datainfo", "diameter:" + diameter);
				if(diameter == 0) return bmp;
				scaledSrcBmp = Bitmap.createScaledBitmap(bmp, diameter, diameter, false);
				Log.i("datainfo", "2");
			}
			else
				scaledSrcBmp = bmp;
		if(scaledSrcBmp == null) return null;
		Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight(),
				Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());

		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(Color.parseColor("#BAB399"));
		canvas.drawCircle(scaledSrcBmp.getWidth() / 2,
				scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);

		return output;
	}

}
